<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;
    $('fbox_productsGrid .ui-reset , #refresh_productsGrid').click(function(){
        $("#productsGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#productsGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#productsGrid").trigger("reloadGrid");
    });

    $.subscribe('searchProductsGrid', function(event,data) {
        $("#productsGrid").jqGrid('searchGrid', {closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search Products"} );
    });
    
    $.subscribe('productsGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#productsGrid_edit" ).button( "option", "disabled", false );
        $( "#productsGrid_delete" ).button( "option", "disabled", false );
    });
    
    $.subscribe('addproductsGrid', function(event,data) {
        $("#productsGrid").jqGrid('editGridRow',"new",{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:false,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editproductsGrid', function(event,data) {
        if(rowId!=0){
            $("#productsGrid").jqGrid('editGridRow',rowId,{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:false,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select a product first.");
        }
    });

    $.subscribe('deleteproductsGrid',function(event,data){
        if(rowId!=0){
            $("#productsGrid").jqGrid('delGridRow',rowId,{resize:true,drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:false,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select a product first.");
        }
    });
    
    $.subscribe('productsGridComplete',function(event,data){
        $( "#productsGrid_search" ).button( "option", "disabled", false );
        $( "#productsGrid_add" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,''];
    }

</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="productsGrid_search" onClickTopics="searchProductsGrid" value="Search Products"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="productsGrid_add" onClickTopics="addproductsGrid" value="Add Products"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="productsGrid_edit" onClickTopics="editproductsGrid" value="Edit Products"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="productsGrid_delete" onClickTopics="deleteproductsGrid" value="Delete Products"/> 
</div>
<s:url id="remoteurl" action="grid-products"/>
<s:url id="editurl" action="edit-products"/>

<sjg:grid
    id="productsGrid"
    caption="Cashmate Products"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    pager="true"
    pagerButtons="false"
    pagerInput="false"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="true"
    navigatorSearchOptions="{closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:'Search Product'}"
    navigatorView="true"
    onSelectRowTopics="productsGridRowSelected"
    onGridCompleteTopics="productsGridComplete"
    >

    <sjg:gridColumn name="productid"
                    index="productid"
                    title="Products ID"
                    sortable="false"
                    key="true"
                    hidden="true"
                    />

    <sjg:gridColumn name="productname"
                    index="productname"
                    title="Product Name"
                    sortable="true"
                    align="center"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    editable="true"
                    edittype="text"
                    editoptions="{maxlength:'11'}"
                    editrules="{required:true,number:true}"
                    />

    <sjg:gridColumn name="productdesc"
                    index="productdesc"
                    title="Product Description"
                    sortable="true"
                    align="center"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    editable="true"
                    edittype="text"
                    editrules="{required:true}"
                    />




</sjg:grid>